package com.t01.bdqn.servlet;

import com.alibaba.fastjson.JSON;
import com.t01.bdqn.entity.ComputerRoom;
import com.t01.bdqn.entity.dto.ComputerRoomQuery;
import com.t01.bdqn.service.ComputerRoomService;
import com.t01.bdqn.service.impl.ComputerRoomServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/computerRoomServlet")
@MultipartConfig
public class ComputerRoomServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/json;charset=utf-8");
        ComputerRoomService service = new ComputerRoomServiceImpl();
        PrintWriter out = resp.getWriter();
        String opr = req.getParameter("opr");


        if ("ComputerSet".equals(opr)) {

            ComputerRoomQuery computerRoomQuery = new ComputerRoomQuery();
            if(null!=req.getParameter("page") && !"".equals(req.getParameter("page"))){
                computerRoomQuery.setPage( Integer.parseInt(req.getParameter("page")));
            }
            if(null!=req.getParameter("limit") && !"".equals(req.getParameter("limit"))){
                computerRoomQuery.setLimit(Integer.parseInt(req.getParameter("limit")));
            }
            if(null!=req.getParameter("roomName") && !"".equals(req.getParameter("roomName"))){
                computerRoomQuery.setRoomName(req.getParameter("roomName").toString());
            }
            if(null!=req.getParameter("state") && !"".equals(req.getParameter("state"))){
                computerRoomQuery.setState(req.getParameter("state").toString());
            }
            if(null!=req.getParameter("location") && !"".equals(req.getParameter("location"))){
                computerRoomQuery.setLocation(req.getParameter("location").toString());
            }

            //start 开始条数
            computerRoomQuery.setStart((computerRoomQuery.getPage()-1)*computerRoomQuery.getLimit());
            List<ComputerRoom> list = null;

            try {
                list = service.ComputerSet(computerRoomQuery);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }

            Map<String, Object> mapResult = new HashMap<>();

            mapResult.put("code", 0);
            mapResult.put("msg", "");
            mapResult.put("count", 2000);
            mapResult.put("data", list);

            out.print(JSON.toJSONString(mapResult));
            out.flush();
            out.close();

        }

    }
}
